<?php
defined('_JEXEC') or die('Restricted access');

class igprofileController extends igbaseController
{
	protected $redirect = 'index.php?option=com_igallery&controller=igprofile';
	
	function __construct($config = array())
	{
		parent::__construct($config);
	
		$this->registerTask('apply', 'save');
	}
	
	function add_profile_redirect()
	{
		//the add button uses post to navigate, this is more RESTful
		$this->setRedirect('index.php?option=com_igallery&controller=igprofile&view=add_edit_profile');
	}
	
	function save()
	{
		$post	= JRequest::get('post');
		
		//if the was a watermark image uploaded, process it
		if(strlen($_FILES['watermark_filename']['name']) > 2 )
		{   
			$fileName = $_FILES['watermark_filename']['name'];
			$tmpPath = $_FILES['watermark_filename']['tmp_name'];
			$uploadError = $_FILES['watermark_filename']['error'];
			
			if(!$uploadedFile = igUploadHelper::upload_file($fileName, $tmpPath, $uploadError, IG_WATERMARK_PATH, true) )
			{
				return false;
			}
			
			$post['watermark_filename'] = $uploadedFile;
		}
		
		$model = $this->getModel();
		
		if( !$model->save($post) )
		{
			JError::raise(2, 500, $model->getError() );
		}
		else 
		{
			$msg = JText::_('PROFILE SAVED');
		} 
		
		switch($this->_task)
		{
			case 'apply':
			$cid = JRequest::getVar('cid', array(0), '', 'array');
			$id = (int)$cid[0];
			$url = 'index.php?option=com_igallery&controller=igprofile&view=add_edit_profile&cid='.$id.'&startOffset='.$post['startOffset'];
			break;
			
			case 'save':
			$url = 'index.php?option=com_igallery&controller=igprofile';
		}
	
		$this->setRedirect($url, JText::_('PROFILE SAVED'));
	}
	
	function remove()
	{
		$model = $this->getModel();
		
		if( !$model->checkAssigned() )
		{
			$this->setRedirect('index.php?option=com_igallery&controller=igprofile');
			return;
		}
		
		if( !$model->delete() )
		{
			JError::raise(2, 500, $model->getError() );
		}
		
		$this->setRedirect('index.php?option=com_igallery&controller=igprofile',JText::_('SUCCESSFULLY DELETED'));
	}
    
}